/**
 *  通过本文件导出的default*若干对象用于redux构建
 *  初始state。
 */

import { fromJS } from 'immutable';
import { isMobile } from '../utils/utils';

let __DEFAULT__ = window.__DEFAULT__;

if (__DEFAULT__) {
    __DEFAULT__.ds.defaultUserState.unrender.isWX = /MicroMessenger/.test(navigator.userAgent);

    if (__DEFAULT__.ds.defaultUserState.unrender.needchange) {
        __DEFAULT__.ds.defaultUserState.unrender.isSpread = isMobile ? false : true;
    }

    var defaultBookState_RAW = fromJS(__DEFAULT__.ds_raw.defaultBookState);
    var defaultUserState_RAW = fromJS(__DEFAULT__.ds_raw.defaultUserState);
    var defaultAsideState_RAW = fromJS(__DEFAULT__.ds_raw.defaultAsideState);
    var defaultMessageState_RAW = fromJS(__DEFAULT__.ds_raw.defaultMessageState);

    var defaultBookState = fromJS(__DEFAULT__.ds.defaultBookState);
    var defaultUserState = fromJS(__DEFAULT__.ds.defaultUserState);
    var defaultAsideState = fromJS(__DEFAULT__.ds.defaultAsideState);
    var defaultMessageState = fromJS(__DEFAULT__.ds.defaultMessageState);

} else {
    var defaultBookState = fromJS({
        render: {
            all: undefined,
            allpage: undefined,
            keywords: undefined,
            nowpage: undefined,
            timestamp: undefined,
            books: []
        },
        unrender: {
            // keyword字段承担两个作用：
            // 1. 在每次搜索后，分页器需要根据这个字段请求下一页
            // 2. 从微信进来的页面需要根据这个字段发请求
            keyword: undefined,
            // 根据微信的关键字搜索图书后，查看某本书的详情，此时
            // 界面滚动到书所在的位置，offset为书的偏移量
            offset: undefined,
            rec_books: []
        }
    });

    var defaultUserState = fromJS({
        render: {
            bind: undefined,
            book: [],
            canrenew: undefined,
            count: undefined,
            subscibe: [],
            timestamp: undefined,
            usercookie: undefined,
            user: undefined
        },
        unrender: {
            isLogin: false,
            // 控制userpanel是否展开，移动端默认不展开
            isSpread: isMobile ? false : true,
            // 浏览器环境是否为微信
            isWX: /MicroMessenger/.test(navigator.userAgent),
            // 默认情况下没有WXID，当cookie中有OpenID时此字段被填充
            WXID: undefined,
            // 当环境为微信且未绑定畅校园时，通过此字段显示绑定按钮
            isGoBind: false,
            // 当前用户是否绑定了畅校园， true/false
            isBind: undefined
        }
    });

    var defaultAsideState = fromJS({
        render: {
            book: undefined,
            isLike: false,
            douban: {
                isbn: undefined,
                summary: undefined,
                link: undefined
            }
        },
        unrender: {
            // 控制aside是否展开，默认不展开
            isSpread: false
        }
    });

    var defaultMessageState = fromJS({
        'text': null,
        'isShow': false,
        // TODO: action字段功能待补充
        'action': undefined
    });

    var defaultBookState_RAW = defaultBookState;
    var defaultUserState_RAW = defaultUserState;
    var defaultAsideState_RAW = defaultAsideState;
    var defaultMessageState_RAW = defaultMessageState;
}

document.getElementById('defstate').remove();

export {
defaultBookState,
defaultUserState,
defaultAsideState,
defaultMessageState,
defaultBookState_RAW,
defaultUserState_RAW,
defaultAsideState_RAW,
defaultMessageState_RAW
};
